import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:niuniu_lulu/index/page/groups_widget.dart';

typedef InputValueChanged = Function(InputControllers4 value);
class InputControllers4 with ChangeNotifier{
  TextEditingController controllerA = TextEditingController();
  TextEditingController controllerB = TextEditingController();
  TextEditingController controllerC = TextEditingController();
  TextEditingController controllerD = TextEditingController();
  TextEditingController controllerDD = TextEditingController();
  GroupType type = GroupType.GroupFive;
  InputValueChanged inputValueChanged;

  void computeBian(){
    if (type != GroupType.Group6) {return;}
    if (controllerB.text.length != 0 && controllerC.text.length != 0) {
      if (double.parse(controllerB.text) is double && double.parse(controllerC.text) is double) {
        if (double.parse(controllerC.text) > 0) {
          var temp = double.parse(controllerC.text) / double.parse(controllerB.text);
          controllerD.text = (sqrt(temp) - 1).toStringAsFixed(4);
        } else {
          controllerD.text = '';
        }
      }
    }
  }

  void oberseverControllersChange(GroupType type){
    this.type = type;
    controllerB.removeListener(() { });
    controllerC.removeListener(() { });
    controllerA.addListener(() {
      notifyListeners();
      if(this.inputValueChanged != null){
        this.inputValueChanged(this);
      }
    });

    controllerB.addListener(() {
      if (type == GroupType.Group6){
        computeBian();
      }
      if(this.inputValueChanged != null){
        this.inputValueChanged(this);
      }
      notifyListeners();
    });
    controllerC.addListener(() {
      if (type == GroupType.Group6){
        computeBian();
      }
      notifyListeners();
    });
    controllerD.addListener(() {
      notifyListeners();
    });
    controllerDD.addListener(() {
      notifyListeners();
    });
  }
}